package com.wss.common.base;

import butterknife.BindComponent;
import cn.bingoogolapple.refreshlayout.BgaStickinessRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BgarefreshLayout;
import com.wss.common.base.mvp.BasePresenter;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.ListContainer;

/**
 * Describe：带下拉刷新 上拉加载更多的Fragment
 * 内部实现为刷新控件 SmartRefreshLayout + 列表控件 RecyclerView
 * Created by 吴天强 on 2018/10/23.
 */
public abstract class BaseRefreshListFragment<P extends BasePresenter> extends BaseMvpFragment<P> {

//    @BindComponent(ResourceTable.Id_ptrl_list)
    BgarefreshLayout refreshLayout;

//    @BindComponent(ResourceTable.Id_recycle_view)
    ListContainer recyclerView;
    /**
     * 适配器
     */
    private BaseItemProvider adapter;

    public BaseRefreshListFragment(Ability ability) {
        super(ability);
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_layout_refresh;
    }

    @Override
    protected void initView() {

        refreshLayout = (BgarefreshLayout) getRootView().findComponentById(ResourceTable.Id_ptrl_list);
        recyclerView = (ListContainer) getRootView().findComponentById(ResourceTable.Id_recycle_view);
        initRefresh();
        adapter = createAdapter();
        refreshLayout.setDelegate(createRefreshListener());
        recyclerView.setItemProvider(adapter);


    }

    private void initRefresh() {

        BgaStickinessRefreshViewHolder stickinessRefreshViewHolder = new BgaStickinessRefreshViewHolder(context, true);
        refreshLayout.setRefreshViewHolder(stickinessRefreshViewHolder);
        refreshLayout.setIsShowLoadingMoreView(true);
        refreshLayout.setPullDownRefreshEnable(true);
        stickinessRefreshViewHolder.setPullDownRefreshText("释放立即刷新...");
        stickinessRefreshViewHolder.setLoadingMoreText("加载更多...");
        stickinessRefreshViewHolder.setUltimateColor(ResourceTable.Color_gray);
        stickinessRefreshViewHolder.setLoadMoreBackgroundColorRes(ResourceTable.Color_light_white);
        stickinessRefreshViewHolder.setRefreshViewBackgroundColorRes(ResourceTable.Color_light_white);
        stickinessRefreshViewHolder.setLoaderTextColor(ResourceTable.Color_gray);
        stickinessRefreshViewHolder.setPullUpDilogueVisible(false);
        stickinessRefreshViewHolder.setLoaderTextSize(40);
        stickinessRefreshViewHolder.setProgressSize(80);
    }

    @Override
    public void dismissLoading() {
        super.dismissLoading();
        stopRefresh();
    }

    @Override
    public void showLoading() {
        super.showLoading();
        hideEmptyView();
    }

    /**
     * 停止刷新
     */
    protected void stopRefresh() {
        refreshLayout.endRefreshing();
        refreshLayout.endLoadingMore();
    }

    /**
     * 获取刷新控件
     *
     * @return PullToRefreshLayout
     */
    protected BgarefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    /**
     * 获取加载数据列表控件
     *
     * @return ListContainer
     */
    protected ListContainer getRecyclerView() {
        return recyclerView;
    }

    /**
     * 返回适配器
     *
     * @return BaseItemProvider
     */
    protected BaseItemProvider getAdapter() {
        return adapter;
    }

    /**
     * 创建刷新监听器
     *
     * @return BgarefreshLayout.BgarefreshLayoutDelegate
     */
    protected abstract BgarefreshLayout.BgarefreshLayoutDelegate createRefreshListener();

    /**
     * 创建列表适配器
     *
     * @return BaseItemProvider
     */
    protected abstract BaseItemProvider createAdapter();
}
